<?php

namespace App\Repository\Information;

use App\Models\Information\InformationOrderModel as model;
use Illuminate\Support\Facades\Cache;
use App\Repository\RoleRepository;
use Illuminate\Support\Facades\DB;
use App\Repository\BaseRepository;
use App\Repository\Product\ProductRepository;
use App\Repository\UploadRepository;
use App\Repository\Product\GoodsRemarkRepository;
use App\Libs\PhpOffice\Excel;
use App\Models\Order\OrderExportModel;

class InformationOrdersRepository extends  BaseRepository
{
  private $model;
  private $productModel;
  public function __construct()
  {
    $this->model = new model();
    $this->productModel = new ProductRepository();
    parent::__construct();
  }
  //列表
  public function index($where)
  {
    $cron = $where;
    if (empty($cron['order'])) {
      // $cron['order'] = ['orderby' => 'id', 'orderdir' => 'desc'];
      $cron['order'] = ['orderby' => ['id' => 'desc']];
    }

    // $cron['where'] = $where;
    // var_dump($cron, 111111111);

    $data = $this->search($this->model, $cron, 1, 9000000);
    if (!empty($data['data'])) {
      $data['data'] = usfirstArr($data['data']);
    }
    return $data;
  }
  //添加
  public function add($data)
  {
    //必填 字段
    $mustField = [
      'name',
      'goods_id',
      'phone',
      'idcard',
      'address',
      'sup_id',
      'sup_name',
      'product_id',
      'shop_id'
    ];
    $optionalFields = $this->model->getTableField();
    $msgField = [
      'name' => '用户名称不能为空',
      'goods_id' => '请选择商品',
      'phone' => '联系方式不能为空',
      'idcard' => '省份证信息不能为空',
      'address' => '详细地址不能为空',
      'sup_id' => '上游编码不能为空',
      'sup_name' => '上游名称不能为空',
      'product_id' => '产品信息不能为空',
      'shop_id' => '店铺信息不能为空'
    ];
    $insertData  = $this->getRealData($data, $mustField, $optionalFields, $msgField);
    if (!empty($insertData) && is_string($insertData)) {
      //返回错误参数
      return $this->returnMsg(
        2,
        $insertData
      );
    }
    $isTrue = $this->getOne(['idcard' => $insertData['idcard'], 'name' => $insertData['name'], 'goods_id' => $insertData['goods_id'], 'phone' => $insertData['phone'], 'shop_id' => $data['shop_id']]);
    if ($isTrue) {
      if ($isTrue->status >= 1 && $isTrue->status <= 11) {
        return $this->returnMsg(5, '运营商审核中');
      }
      if ($isTrue->status > 11 && $isTrue->status <= 40) {
        return $this->returnMsg(5, '已在运输中');
      }
      if ($isTrue->status > 40) {
        return $this->returnMsg(4, !empty($isTrue->three_msg) ? $isTrue->three_msg : '暂不符合办理条件');
      }
      return $this->returnMsg(3, '您已购买该卡品');
    }
    if (!empty($insertData['status'])) {
      $insertData['status_desc'] = $this->model->getOrderStatusDesc($insertData['status']);
    }
    $insertData['created_at'] = date('Y-m-d H:i:s');
    $insertData['shop_id'] = (!empty($data->user->shop_id) ? $data->user->shop_id : (!empty($data->user->id) ? $data->user->id : (!empty($insertData['shop_id']) ? $insertData['shop_id'] : '')));

    $res = $this->model->insertGetId($insertData);

    if ($res) {
      return $this->returnMsg(200, '添加成功');
    }
    return $this->returnMsg(1, '添加失败');
  }
  //删除
  public function del($where)
  {
    $flag = true;
    $msg = '未知错误';
    $code = 1;
    if (empty($where)) {
      $flag = false;
      $msg = '删除条件不能为空';
      $code = 2;
    }
    if ($flag) {
      $res = $this->model->where($where)->get();
      if (!$res) {
        $flag = false;
        $msg = '未查询到数据';
        $code = 3;
      }
    }
    if ($flag) {
      $del = $this->model->where($where)->delete();
      if ($del) {
        $msg = '删除成功';
        $code = 200;
      }
    }
    return $this->returnMsg($code, $msg);
  }
  //修改
  public function edit($where, $data, $isMust = false)
  {
    if (empty($where)) {
      return $this->returnMsg(1, '条件不能为空');
    }
    if (empty($data)) {
      return $this->returnMsg(2, '修改参数不能为空');
    }
    //过滤字段
    // var_dump($data->all());
    //过滤字段
    $optionalFields = $this->model->getTableField();
    $field  = $this->getRealData($data, [], $optionalFields, []);
    //查询是否存在
    $isTrue = $this->model->where($where)->first();
    if (!$isTrue) {
      return $this->returnMsg(3, '未查询到订单');
    }
    // var_dump($field, 1111111111);
    if (!empty($field['status'])) {
      $field['status_desc'] = $this->model->getOrderStatusDesc($field['status']);
    }
    //先查询是否存在
    $res = $this->model->where($where)->update($field);

    //先查询是否存在
    // $res = $this->model->where($where)->update($field);
    if ($isMust && $res) {
      return $this->returnMsg(200, '修改成功');
    }
    if (!$isMust) {
      return $this->returnMsg(200, '修改成功');
    }
    return $this->returnMsg(3, '请重新修改');
  }
  //列表查询
  public function lists($where = [], $page = 1, $limit = 10)
  {
    $res = $this->search($this->model, $where, $page, $limit);

    return $res;
  }
  public function getOne($where, $field = ['*'])
  {
    return $this->model->where($where)->select($field)->first();
  }
  public function getIndexExcel($where)
  {
    $cron = $where;
    if (empty($cron['order'])) {
      // $cron['order'] = ['orderby' => 'id', 'orderdir' => 'desc'];
      $cron['order'] = ['orderby' => ['id' => 'desc']];
    }

    // $cron['where'] = $where;
    // var_dump($cron, 111111111);

    $data = $this->search($this->model, $cron, 1, 9000000);
    return $data;
  }
  public function getExcelPort($id, $params, $name, $shop_id = 0)
  {
    if (!empty($params)) {
      $request = json_decode($params, true);
      $where['where']['shop_id'] = $shop_id;
      if (!empty($request['sup_id'])) {
        $where['In']['sup_id'] = $request['sup_id'];
      }
      if (!empty($request['phone'])) {
        $where['where']['phone'] = trim($request['phone'], ' ');
      }
      if (!empty($request['page_id'])) {
        $where['In']['page_id'] = $request['page_id'];
      }
      if (!empty($request['name'])) {
        $where['where']['name'] = $request['name'];
      }
      if (!empty($request['idcard'])) {
        $where['where']['idcard'] = $request['idcard'];
      }
      if (isset($request['status'])) {
        $where['In']['status'] = $request['status'];
      }

      //时间验证 
      if (!empty($request['dateRange'])) {
        // $time = $request->get('dateRange');
        $where['between'] = ['created_at' => $request['dateRange']];
      }
      if (!empty($request['params'])) {
        // $time = $request->get('dateRange');
        $where['between'] = ['created_at' => [$request['params']['beginTime'], $request['params']['endTime']]];
      }
    }
    $where['limit']['start'] = 0;
    $where['limit']['number'] = 50000;
    // var_dump($where);
    $resData = $this->getIndexExcel($where);
    // var_dump($resData);
    $model = new Excel();
    $title = [
      '订单号',
      '用户名',
      '联系方式',
      '身份证',
      '商品信息',
      '省',
      '市',
      '区',
      '详细地址',
      '上游名称',
      '订单状态',
      '失败原因',
      '下单时间'
    ];
    $excelData = [];

    if (!empty($resData['data'])) {
      foreach ($resData['data'] as $key => $v) {
        if(empty(env('IS_ENCRYP')) || env('IS_ENCRYP') != 2){
          if(!empty($v['idcard'])){
            $v['idcard'] = str_replace(mb_substr($v['idcard'],3,11),'*******',($v['idcard']));
          }
        }
        $excelData[] = [
          $v['ordersno'],
          $v['name'],
          $v['phone'],
          $v['idcard'],
          $v['goods_name'],
          $v['province'],
          $v['city'],
          $v['area'],
          $v['address'],
          $v['sup_name'],
          $v['status_desc'],
          $v['three_msg'],
          $v['created_at'],
        ];
      }
    }

    $fielname = '/attach/excel/1/' . date('Ym') . '/' . $name . date('YmdHis') . '.xlsx';
    $model->exportFile($excelData, $title, $fielname);
    $excelModel = new OrderExportModel();
    $excelModel->where(['id' => $id])->update(['is_run' => 1, 'path' => $fielname]);
    return $this->returnMsg(200, '导出成功', ['fiel' => $fielname]);
  }
}
